Load all required libraries.

library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.3     v purrr   0.3.4
## v tibble  3.1.1     v dplyr   1.0.6
## v tidyr   1.1.3     v stringr 1.4.0
## v readr   1.4.0     v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(broom)

Read in raw data from RDS.

raw_data <- readRDS("./n1_n2_cleaned_cases.rds")

Make a few small modifications to names and data for visualizations.

final_data <- raw_data %>% mutate(log_copy_per_L = log10(mean_copy_num_L)) %>%
  rename(Facility = wrf) %>%
  mutate(Facility = recode(Facility, 
                           "NO" = "WRF A",
                           "MI" = "WRF B",
                           "CC" = "WRF C"))

Seperate the data by gene target to ease layering in the final plot

#make three data layers
only_positives <<- subset(final_data, (!is.na(final_data$Facility)))
only_n1 <- subset(only_positives, target == "N1")
only_n2 <- subset(only_positives, target == "N2")
only_background <<-final_data %>% 
  select(c(date, cases_cum_clarke, new_cases_clarke, X7_day_ave_clarke)) %>%
  group_by(date) %>% summarise_if(is.numeric, mean)

#specify fun colors
background_color <- "#7570B3"
seven_day_ave_color <- "#E6AB02"
marker_colors <- c("N1" = '#1B9E77',"N2" ='#D95F02')
#remove facilty C for now
#only_n1 <- only_n1[!(only_n1$Facility == "WRF C"),]
#only_n2 <- only_n2[!(only_n2$Facility == "WRF C"),]

only_n1 <- only_n1[!(only_n1$Facility == "WRF A" & only_n1$date == "2020-11-02"), ]
only_n2 <- only_n2[!(only_n2$Facility == "WRF A" & only_n2$date == "2020-11-02"), ]

Build the main plot

      #first layer is the background epidemic curve
        p1 <- only_background %>%
              plotly::plot_ly() %>%
              plotly::add_trace(x = ~date, y = ~new_cases_clarke, 
                                type = "bar", 
                                hoverinfo = "text",
                                text = ~paste('</br> Date: ', date,
                                                     '</br> Daily Cases: ', new_cases_clarke),
                                alpha = 0.5,
                                name = "Daily Reported Cases",
                                color = background_color,
                                colors = background_color,
                                showlegend = FALSE) %>%
            layout(yaxis = list(title = "Clarke County Daily Cases", showline=TRUE)) %>%
            layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
        
        #renders the main plot layer two as seven day moving average
        p1 <- p1 %>% plotly::add_trace(x = ~date, y = ~X7_day_ave_clarke, 
                             type = "scatter",
                             mode = "lines",
                             hoverinfo = "text",
                            text = ~paste('</br> Date: ', date,
                                                     '</br> Seven-Day Moving Average: ', X7_day_ave_clarke),
                             name = "Seven Day Moving Average Athens",
                             line = list(color = seven_day_ave_color),
                             showlegend = FALSE)
      

        
        #renders the main plot layer three as positive target hits
        
        p2 <- plotly::plot_ly() %>%
          plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
                                       type = "scatter",
                                       mode = "markers",
                                       hoverinfo = "text",
                                       text = ~paste('</br> Date: ', date,
                                                     '</br> Facility: ', Facility,
                                                     '</br> Target: ', target,
                                                     '</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
                                       data = only_n1,
                                       symbol = ~Facility,
                                       marker = list(color = '#1B9E77', size = 8, opacity = 0.65),
                                       showlegend = FALSE) %>%
          plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
                                       type = "scatter",
                                       mode = "markers",
                                       hoverinfo = "text",
                                       text = ~paste('</br> Date: ', date,
                                                     '</br> Facility: ', Facility,
                                                     '</br> Target: ', target,
                                                     '</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
                                       data = only_n2,
                                       symbol = ~Facility,
                                       marker = list(color = '#D95F02', size = 8, opacity = 0.65),
                                       showlegend = FALSE) %>%
            layout(yaxis = list(title = "SARS CoV-2 Copies/L", 
                                 showline = TRUE,
                                 type = "log",
                                 dtick = 1,
                                 automargin = TRUE)) %>%
            layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
        
        #adds the limit of detection dashed line
        p2 <- p2 %>% plotly::add_segments(x = as.Date("2020-03-14"), 
                                          xend = ~max(date + 10), 
                                          y = 3571.429, yend = 3571.429,
                                          opacity = 0.35,
                                          line = list(color = "black", dash = "dash")) %>%
          layout(annotations = list(x = as.Date("2020-03-28"), y = 3.8, xref = "x", yref = "y", 
                                    text = "Limit of Detection", showarrow = FALSE))

        

        p1
        p2

Combine the two main plot pieces as a subplot

#seperate n1 and n2 frames by site
#n1
wrf_a_only_n1 <- subset(only_n1, Facility == "WRF A")
wrf_b_only_n1 <- subset(only_n1, Facility == "WRF B")
wrf_c_only_n1 <- subset(only_n1, Facility == "WRF C")

#n2
wrf_a_only_n2 <- subset(only_n2, Facility == "WRF A")
wrf_b_only_n2 <- subset(only_n2, Facility == "WRF B")
wrf_c_only_n2 <- subset(only_n2, Facility == "WRF C")


#rejoin the old data frames then seperate in to averages for each plant. 
wrfa_both <- full_join(wrf_a_only_n1, wrf_a_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke", "X7_day_ave_clarke", "Facility", "collection_num", "target", "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies", "sd_total_copies", "day", "log_copy_per_L")
wrfb_both <- full_join(wrf_b_only_n1, wrf_b_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke", "X7_day_ave_clarke", "Facility", "collection_num", "target", "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies", "sd_total_copies", "day", "log_copy_per_L")
wrfc_both <- full_join(wrf_c_only_n1, wrf_c_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke", "X7_day_ave_clarke", "Facility", "collection_num", "target", "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies", "sd_total_copies", "day", "log_copy_per_L")
#get max date
maxdate <- max(wrfa_both$date)
mindate <- min(wrfa_both$date)

Build loess smoothing figures figures

This makes the individual plots

#**************************************WRF A PLOT**********************************************
#add trendlines 
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_botha <- ggplot(wrfa_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_botha<<-..y..), method = "loess", color = '#1B9E77', 
              span = 0.6, n = 372)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_botha
## `geom_smooth()` using formula 'y ~ x'

fit_botha
##   [1] 13.05610 13.05117 13.04632 13.04154 13.03682 13.03217 13.02758 13.02306
##   [9] 13.01859 13.01418 13.00983 13.00553 13.00129 12.99710 12.99296 12.98886
##  [17] 12.98482 12.98082 12.97686 12.97294 12.96907 12.96523 12.96143 12.95766
##  [25] 12.95393 12.95023 12.94656 12.94292 12.93931 12.93572 12.93216 12.92861
##  [33] 12.92509 12.92159 12.91810 12.91463 12.91118 12.90776 12.90436 12.90100
##  [41] 12.89767 12.89438 12.89112 12.88791 12.88473 12.88160 12.87852 12.87548
##  [49] 12.87250 12.86956 12.86668 12.86385 12.86109 12.85838 12.85573 12.85315
##  [57] 12.85064 12.84819 12.84581 12.84350 12.84127 12.83912 12.83704 12.83504
##  [65] 12.83312 12.83129 12.82954 12.82789 12.82632 12.82484 12.82346 12.82214
##  [73] 12.82084 12.81956 12.81832 12.81710 12.81591 12.81476 12.81364 12.81255
##  [81] 12.81151 12.81051 12.80955 12.80864 12.80777 12.80695 12.80619 12.80548
##  [89] 12.80482 12.80422 12.80368 12.80320 12.80278 12.80243 12.80214 12.80193
##  [97] 12.80178 12.80171 12.80172 12.80180 12.80196 12.80220 12.80252 12.80293
## [105] 12.80343 12.80402 12.80470 12.80547 12.80633 12.80730 12.80836 12.80952
## [113] 12.81079 12.81224 12.81396 12.81592 12.81811 12.82051 12.82311 12.82589
## [121] 12.82884 12.83193 12.83516 12.83851 12.84196 12.84549 12.84910 12.85275
## [129] 12.85645 12.86017 12.86389 12.86761 12.87130 12.87495 12.87854 12.88206
## [137] 12.88549 12.88881 12.89201 12.89508 12.89917 12.90532 12.91330 12.92292
## [145] 12.93397 12.94622 12.95948 12.97352 12.98815 13.00315 13.01830 13.03340
## [153] 13.04825 13.06261 13.07630 13.08909 13.10078 13.11115 13.12000 13.12711
## [161] 13.13228 13.13741 13.14441 13.15306 13.16314 13.17444 13.18674 13.19983
## [169] 13.21348 13.22748 13.24161 13.25566 13.26941 13.28264 13.29514 13.30668
## [177] 13.31706 13.32605 13.33344 13.33901 13.34254 13.34382 13.34351 13.34244
## [185] 13.34062 13.33807 13.33482 13.33087 13.32626 13.32099 13.31508 13.30857
## [193] 13.30145 13.29376 13.28550 13.27671 13.26739 13.25757 13.24726 13.23649
## [201] 13.22527 13.21361 13.20155 13.18910 13.17627 13.16308 13.14956 13.13573
## [209] 13.12159 13.10717 13.09249 13.07757 13.06242 13.04706 13.03152 13.01581
## [217] 12.99995 12.98395 12.96784 12.95164 12.93536 12.91903 12.90265 12.88626
## [225] 12.86831 12.84745 12.82399 12.79823 12.77046 12.74098 12.71011 12.67812
## [233] 12.64534 12.61205 12.57857 12.54517 12.51218 12.47989 12.44859 12.41859
## [241] 12.39020 12.36370 12.33940 12.31760 12.29860 12.28108 12.26351 12.24595
## [249] 12.22842 12.21095 12.19359 12.17636 12.15930 12.14244 12.12582 12.10947
## [257] 12.09343 12.07773 12.06241 12.04749 12.03301 12.01928 12.00655 11.99476
## [265] 11.98386 11.97380 11.96453 11.95599 11.94814 11.94092 11.93428 11.92818
## [273] 11.92255 11.91734 11.91252 11.90801 11.90378 11.89976 11.89592 11.89219
## [281] 11.88853 11.88488 11.88119 11.87741 11.87349 11.86938 11.86502 11.86036
## [289] 11.85536 11.84996 11.84410 11.83775 11.83084 11.82332 11.81514 11.80626
## [297] 11.79705 11.78795 11.77893 11.76999 11.76112 11.75230 11.74353 11.73478
## [305] 11.72606 11.71735 11.70863 11.69990 11.69115 11.68236 11.67352 11.66463
## [313] 11.65567 11.64662 11.63748 11.62824 11.61889 11.60951 11.60022 11.59099
## [321] 11.58181 11.57268 11.56358 11.55450 11.54543 11.53636 11.52727 11.51815
## [329] 11.50899 11.49978 11.49052 11.48117 11.47174 11.46222 11.45258 11.44282
## [337] 11.43293 11.42295 11.41292 11.40284 11.39272 11.38255 11.37233 11.36207
## [345] 11.35177 11.34143 11.33104 11.32061 11.31014 11.29963 11.28908 11.27850
## [353] 11.26787 11.25720 11.24650 11.23576 11.22499 11.21418 11.20333 11.19245
## [361] 11.18154 11.17060 11.15962 11.14861 11.13757 11.12650 11.11540 11.10427
## [369] 11.09311 11.08193 11.07071 11.05948
#assign fits to a vector
both_trenda <- fit_botha

#extract y min and max for each
limits_botha <- ggplot_build(extract_botha)$data
## `geom_smooth()` using formula 'y ~ x'
limits_botha <- as.data.frame(limits_botha)
both_ymina <- limits_botha$ymin
both_ymaxa <- limits_botha$ymax

#reassign dataframes (just to be safe)
work_botha <- wrfa_both

#fill in missing dates to smooth fits
work_botha <- work_botha %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_botha <- work_botha$date

#create a new smooth dataframe to layer
smooth_frame_botha <- data.frame(date_vec_botha, both_trenda, both_ymina, both_ymaxa)
#WRF A
#plot smooth frames
p_wrf_a <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_botha, y = ~both_trenda,
                    data = smooth_frame_botha,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_botha,
                                  '</br> Median Log Copies: ', round(both_trenda, digits = 2)),
                    line = list(color = '#1B9E77', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_botha, ymin = ~both_ymina, ymax = ~both_ymaxa,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_botha, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxa, digits = 2),
                                  '</br> Min Log Copies: ', round(both_ymina, digits = 2)),
                    name = "",
                    fillcolor = '#1B9E77',
                    line = list(color = '#1B9E77')) %>%
                layout(yaxis = list(title = "Total Log SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF A") %>%
    plotly::add_segments(x = as.Date("2020-06-24"), 
                                          xend = as.Date("2020-06-24"), 
                                          y = ~min(both_ymina), yend = ~max(both_ymaxa),
                                          opacity = 0.35,
                                          name = "Bars Repoen",
                                          hoverinfo = "text",
                                          text = "</br> Bars Reopen",
                                                 "</br> 2020-06-24",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
    plotly::add_segments(x = as.Date("2020-07-09"), 
                                          xend = as.Date("2020-07-09"), 
                                          y = ~min(both_ymina), yend = ~max(both_ymaxa),
                                          opacity = 0.35,
                                          name = "Mask Mandate",
                                          hoverinfo = "text",
                                          text = "</br> Mask Mandate",
                                                 "</br> 2020-07-09",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
    plotly::add_segments(x = as.Date("2020-08-20"), 
                                          xend = as.Date("2020-08-20"), 
                                          y = ~min(both_ymina), yend = ~max(both_ymaxa),
                                          opacity = 0.35,
                                          name = "</br> Classes Begin",
                                                 "</br> 2020-08-20",
                                          hoverinfo = "text",
                                          text = "Classes Begin",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
        plotly::add_segments(x = as.Date("2020-10-03"), 
                                          xend = as.Date("2020-10-03"), 
                                          y = ~min(both_ymina), yend = ~max(both_ymaxa),
                                          opacity = 0.35,
                                          name = "</br> First Home Football Game",
                                                 "</br> 2020-10-03",
                                          hoverinfo = "text",
                                          text = "First Home Football Game",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfa_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#1B9E77', size = 6, opacity = 0.65))

p_wrf_a
save(p_wrf_a, file = "./plotly_objs/p_wrf_a.rda")
#**************************************WRF B PLOT**********************************************
#add trendlines 
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_bothb <- ggplot(wrfb_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_bothb<<-..y..), method = "loess", color = '#D95F02', 
              span = 0.6, n = 372)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothb
## `geom_smooth()` using formula 'y ~ x'

fit_bothb
##   [1] 12.62165 12.61714 12.61271 12.60838 12.60414 12.59999 12.59593 12.59195
##   [9] 12.58806 12.58426 12.58055 12.57692 12.57337 12.56991 12.56653 12.56324
##  [17] 12.56002 12.55689 12.55384 12.55087 12.54797 12.54516 12.54242 12.53976
##  [25] 12.53718 12.53467 12.53223 12.52987 12.52758 12.52537 12.52323 12.52116
##  [33] 12.51916 12.51723 12.51536 12.51357 12.51184 12.51018 12.50859 12.50706
##  [41] 12.50560 12.50421 12.50290 12.50165 12.50049 12.49940 12.49838 12.49745
##  [49] 12.49659 12.49582 12.49513 12.49453 12.49401 12.49358 12.49324 12.49299
##  [57] 12.49283 12.49276 12.49279 12.49291 12.49314 12.49346 12.49388 12.49440
##  [65] 12.49503 12.49575 12.49659 12.49753 12.49858 12.49975 12.50102 12.50244
##  [73] 12.50404 12.50583 12.50778 12.50990 12.51219 12.51463 12.51723 12.51998
##  [81] 12.52287 12.52590 12.52906 12.53236 12.53577 12.53931 12.54297 12.54673
##  [89] 12.55061 12.55458 12.55865 12.56281 12.56705 12.57138 12.57579 12.58027
##  [97] 12.58481 12.58942 12.59409 12.59881 12.60357 12.60839 12.61324 12.61812
## [105] 12.62303 12.62797 12.63293 12.63790 12.64289 12.64788 12.65286 12.65785
## [113] 12.66283 12.66803 12.67366 12.67969 12.68606 12.69274 12.69969 12.70685
## [121] 12.71420 12.72168 12.72925 12.73688 12.74451 12.75211 12.75963 12.76703
## [129] 12.77427 12.78131 12.78810 12.79460 12.80077 12.80656 12.81194 12.81781
## [137] 12.82505 12.83350 12.84302 12.85346 12.86469 12.87656 12.88893 12.90165
## [145] 12.91458 12.92758 12.94050 12.95320 12.96554 12.97738 12.98856 12.99896
## [153] 13.00842 13.01680 13.02395 13.02975 13.03642 13.04607 13.05833 13.07283
## [161] 13.08922 13.10712 13.12618 13.14602 13.16629 13.18661 13.20663 13.22598
## [169] 13.24429 13.26120 13.27635 13.28936 13.29989 13.30755 13.31199 13.31417
## [177] 13.31535 13.31553 13.31475 13.31304 13.31044 13.30696 13.30263 13.29750
## [185] 13.29157 13.28489 13.27749 13.26938 13.26061 13.25119 13.24116 13.23055
## [193] 13.21939 13.20770 13.19551 13.18286 13.16977 13.15627 13.14239 13.12815
## [201] 13.11360 13.09875 13.08364 13.06829 13.05273 13.03700 13.02111 13.00511
## [209] 12.98902 12.97286 12.95667 12.94048 12.92431 12.90819 12.89216 12.87624
## [217] 12.86046 12.84484 12.82943 12.81276 12.79353 12.77202 12.74851 12.72326
## [225] 12.69654 12.66862 12.63979 12.61031 12.58045 12.55048 12.52069 12.49133
## [233] 12.46268 12.43501 12.40860 12.38371 12.36063 12.33961 12.32094 12.30488
## [241] 12.29031 12.27593 12.26174 12.24775 12.23397 12.22039 12.20704 12.19392
## [249] 12.18102 12.16836 12.15595 12.14378 12.13188 12.12023 12.10885 12.09775
## [257] 12.08693 12.07640 12.06616 12.05729 12.05070 12.04616 12.04347 12.04239
## [265] 12.04270 12.04418 12.04660 12.04976 12.05341 12.05735 12.06135 12.06519
## [273] 12.06863 12.07148 12.07349 12.07445 12.07414 12.07233 12.06880 12.06333
## [281] 12.05733 12.05226 12.04799 12.04437 12.04126 12.03853 12.03603 12.03363
## [289] 12.03118 12.02855 12.02559 12.02217 12.01815 12.01338 12.00773 12.00106
## [297] 11.99383 11.98660 11.97935 11.97207 11.96476 11.95739 11.94996 11.94246
## [305] 11.93488 11.92719 11.91940 11.91150 11.90346 11.89528 11.88695 11.87845
## [313] 11.86978 11.86092 11.85187 11.84260 11.83312 11.82351 11.81387 11.80419
## [321] 11.79446 11.78466 11.77479 11.76482 11.75476 11.74458 11.73427 11.72383
## [329] 11.71324 11.70248 11.69155 11.68044 11.66912 11.65759 11.64584 11.63386
## [337] 11.62162 11.60919 11.59659 11.58385 11.57096 11.55791 11.54472 11.53138
## [345] 11.51789 11.50425 11.49047 11.47654 11.46247 11.44825 11.43389 11.41939
## [353] 11.40474 11.38996 11.37504 11.35997 11.34477 11.32944 11.31396 11.29835
## [361] 11.28261 11.26673 11.25072 11.23457 11.21830 11.20189 11.18536 11.16869
## [369] 11.15190 11.13498 11.11793 11.10076
#assign fits to a vector
both_trendb <- fit_bothb

#extract y min and max for each
limits_bothb <- ggplot_build(extract_bothb)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothb <- as.data.frame(limits_bothb)
both_yminb <- limits_bothb$ymin
both_ymaxb <- limits_bothb$ymax

#reassign dataframes (just to be safe)
work_bothb <- wrfb_both

#fill in missing dates to smooth fits
work_bothb <- work_bothb %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothb <- work_bothb$date

#create a new smooth dataframe to layer
smooth_frame_bothb <- data.frame(date_vec_bothb, both_trendb, both_yminb, both_ymaxb)
#WRF B
#plot smooth frames
p_wrf_b <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_bothb, y = ~both_trendb,
                    data = smooth_frame_bothb,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothb,
                                  '</br> Median Log Copies: ', round(both_trendb, digits = 2)),
                    line = list(color = '#D95F02', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothb, ymin = ~both_yminb, ymax = ~both_ymaxb,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothb, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxb, digits = 2),
                                  '</br> Min Log Copies: ', round(both_yminb, digits = 2)),
                    name = "",
                    fillcolor = '#D95F02',
                    line = list(color = '#D95F02')) %>%
                layout(yaxis = list(title = "Total Log SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF B") %>%
    plotly::add_segments(x = as.Date("2020-06-24"), 
                                          xend = as.Date("2020-06-24"), 
                                          y = ~min(both_yminb), yend = ~max(both_ymaxb),
                                          opacity = 0.35,
                                          name = "Bars Repoen",
                                          hoverinfo = "text",
                                          text = "</br> Bars Reopen",
                                                 "</br> 2020-06-24",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
    plotly::add_segments(x = as.Date("2020-07-09"), 
                                          xend = as.Date("2020-07-09"), 
                                          y = ~min(both_yminb), yend = ~max(both_ymaxb),
                                          opacity = 0.35,
                                          name = "Mask Mandate",
                                          hoverinfo = "text",
                                          text = "</br> Mask Mandate",
                                                 "</br> 2020-07-09",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
    plotly::add_segments(x = as.Date("2020-08-20"), 
                                          xend = as.Date("2020-08-20"), 
                                          y = ~min(both_yminb), yend = ~max(both_ymaxb),
                                          opacity = 0.35,
                                          name = "</br> Classes Begin",
                                                 "</br> 2020-08-20",
                                          hoverinfo = "text",
                                          text = "Classes Begin",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
        plotly::add_segments(x = as.Date("2020-10-03"), 
                                          xend = as.Date("2020-10-03"), 
                                          y = ~min(both_yminb), yend = ~max(both_ymaxb),
                                          opacity = 0.35,
                                          name = "</br> First Home Football Game",
                                                 "</br> 2020-10-03",
                                          hoverinfo = "text",
                                          text = "First Home Football Game",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfb_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#D95F02', size = 6, opacity = 0.65))

p_wrf_b
save(p_wrf_b, file = "./plotly_objs/p_wrf_b.rda")

#**************************************WRF C PLOT********************************************** #add trendlines #extract data from geom_smooth # *********************************span 0.6*********************************** #*****************Must always update the n = TOTAL NUMBER OF DAYS*************************

extract_bothc <- ggplot(wrfc_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_bothc<<-..y..), method = "loess", color = '#E7298A', 
              span = 0.6, n = 372)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothc
## `geom_smooth()` using formula 'y ~ x'

fit_bothc
##   [1] 12.06120 12.05425 12.04741 12.04068 12.03406 12.02755 12.02115 12.01485
##   [9] 12.00866 12.00257 11.99658 11.99069 11.98490 11.97920 11.97360 11.96809
##  [17] 11.96268 11.95735 11.95211 11.94697 11.94190 11.93693 11.93203 11.92722
##  [25] 11.92249 11.91783 11.91325 11.90875 11.90433 11.89997 11.89569 11.89147
##  [33] 11.88733 11.88325 11.87924 11.87529 11.87140 11.86757 11.86381 11.86010
##  [41] 11.85644 11.85285 11.84930 11.84583 11.84247 11.83920 11.83603 11.83297
##  [49] 11.83001 11.82715 11.82439 11.82174 11.81919 11.81675 11.81441 11.81217
##  [57] 11.81004 11.80802 11.80610 11.80428 11.80258 11.80098 11.79948 11.79810
##  [65] 11.79682 11.79565 11.79459 11.79364 11.79279 11.79206 11.79144 11.79093
##  [73] 11.79053 11.79024 11.79006 11.78999 11.79003 11.79019 11.79039 11.79055
##  [81] 11.79069 11.79082 11.79093 11.79104 11.79116 11.79129 11.79144 11.79162
##  [89] 11.79183 11.79209 11.79239 11.79275 11.79318 11.79368 11.79426 11.79492
##  [97] 11.79568 11.79654 11.79751 11.79859 11.79980 11.80114 11.80262 11.80424
## [105] 11.80602 11.80796 11.81006 11.81234 11.81481 11.81746 11.82031 11.82337
## [113] 11.82663 11.83012 11.83383 11.83778 11.84197 11.84641 11.85111 11.85607
## [121] 11.86172 11.86843 11.87609 11.88462 11.89391 11.90388 11.91442 11.92545
## [129] 11.93686 11.94856 11.96047 11.97247 11.98448 11.99639 12.00813 12.01958
## [137] 12.03066 12.04128 12.05132 12.06071 12.07144 12.08537 12.10217 12.12152
## [145] 12.14309 12.16657 12.19162 12.21793 12.24516 12.27300 12.30112 12.32919
## [153] 12.35689 12.38390 12.40990 12.43455 12.45753 12.47853 12.49721 12.51325
## [161] 12.52632 12.53941 12.55546 12.57412 12.59505 12.61792 12.64235 12.66803
## [169] 12.69459 12.72170 12.74900 12.77615 12.80281 12.82863 12.85326 12.87636
## [177] 12.89758 12.91658 12.93301 12.94653 12.95679 12.96344 12.96729 12.96944
## [185] 12.96993 12.96883 12.96619 12.96209 12.95658 12.94973 12.94159 12.93222
## [193] 12.92169 12.91006 12.89738 12.88373 12.86916 12.85372 12.83750 12.82053
## [201] 12.80289 12.78464 12.76584 12.74654 12.72681 12.70672 12.68631 12.66566
## [209] 12.64483 12.62387 12.60284 12.58181 12.56085 12.54000 12.51933 12.49891
## [217] 12.47878 12.45903 12.43969 12.42085 12.40255 12.38486 12.36784 12.35155
## [225] 12.33445 12.31511 12.29376 12.27063 12.24598 12.22002 12.19299 12.16513
## [233] 12.13667 12.10785 12.07891 12.05007 12.02158 11.99366 11.96655 11.94050
## [241] 11.91572 11.89246 11.87096 11.85144 11.83415 11.81844 11.80346 11.78914
## [249] 11.77540 11.76214 11.74931 11.73681 11.72456 11.71248 11.70050 11.68853
## [257] 11.67649 11.66431 11.65189 11.63917 11.62605 11.61301 11.60051 11.58855
## [265] 11.57710 11.56613 11.55562 11.54556 11.53590 11.52664 11.51774 11.50919
## [273] 11.50095 11.49302 11.48536 11.47794 11.47076 11.46377 11.45697 11.45032
## [281] 11.44380 11.43739 11.43107 11.42481 11.41859 11.41238 11.40616 11.39991
## [289] 11.39360 11.38722 11.38073 11.37412 11.36735 11.36042 11.35328 11.34593
## [297] 11.33852 11.33123 11.32407 11.31703 11.31010 11.30330 11.29661 11.29003
## [305] 11.28356 11.27721 11.27097 11.26483 11.25880 11.25287 11.24704 11.24131
## [313] 11.23569 11.23015 11.22472 11.21938 11.21412 11.20898 11.20397 11.19909
## [321] 11.19433 11.18968 11.18515 11.18073 11.17642 11.17220 11.16809 11.16406
## [329] 11.16013 11.15628 11.15251 11.14882 11.14521 11.14166 11.13817 11.13475
## [337] 11.13139 11.12810 11.12494 11.12188 11.11894 11.11611 11.11340 11.11080
## [345] 11.10831 11.10594 11.10368 11.10154 11.09951 11.09760 11.09581 11.09413
## [353] 11.09256 11.09111 11.08978 11.08857 11.08747 11.08648 11.08562 11.08487
## [361] 11.08424 11.08373 11.08333 11.08305 11.08289 11.08285 11.08293 11.08313
## [369] 11.08344 11.08387 11.08443 11.08510
#assign fits to a vector
both_trendc <- fit_bothc

#extract y min and max for each
limits_bothc <- ggplot_build(extract_bothc)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothc <- as.data.frame(limits_bothc)
both_yminc <- limits_bothc$ymin
both_ymaxc <- limits_bothc$ymax

#reassign dataframes (just to be safe)
work_bothc <- wrfc_both

#fill in missing dates to smooth fits
work_bothc <- work_bothc %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothc <- work_bothc$date

#create a new smooth dataframe to layer
smooth_frame_bothc <- data.frame(date_vec_bothc, both_trendc, both_yminc, both_ymaxc)
#WRF C
#plot smooth frames
p_wrf_c <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_bothc, y = ~both_trendc,
                    data = smooth_frame_bothc,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothc,
                                  '</br> Median Log Copies: ', round(both_trendc, digits = 2)),
                    line = list(color = '#E7298A', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothc, ymin = ~both_yminc, ymax = ~both_ymaxc,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothc, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxc, digits = 2),
                                  '</br> Min Log Copies: ', round(both_yminc, digits = 2)),
                    name = "",
                    fillcolor = '#E7298A',
                    line = list(color = '#E7298A')) %>%
                layout(yaxis = list(title = "Total Log SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF C") %>%
    plotly::add_segments(x = as.Date("2020-06-24"), 
                                          xend = as.Date("2020-06-24"), 
                                          y = ~min(both_yminc), yend = ~max(both_ymaxc),
                                          opacity = 0.35,
                                          name = "Bars Repoen",
                                          hoverinfo = "text",
                                          text = "</br> Bars Reopen",
                                                 "</br> 2020-06-24",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
    plotly::add_segments(x = as.Date("2020-07-09"), 
                                          xend = as.Date("2020-07-09"), 
                                          y = ~min(both_yminc), yend = ~max(both_ymaxc),
                                          opacity = 0.35,
                                          name = "Mask Mandate",
                                          hoverinfo = "text",
                                          text = "</br> Mask Mandate",
                                                 "</br> 2020-07-09",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
    plotly::add_segments(x = as.Date("2020-08-20"), 
                                          xend = as.Date("2020-08-20"), 
                                          y = ~min(both_yminc), yend = ~max(both_ymaxc),
                                          opacity = 0.35,
                                          name = "</br> Classes Begin",
                                                 "</br> 2020-08-20",
                                          hoverinfo = "text",
                                          text = "Classes Begin",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
        plotly::add_segments(x = as.Date("2020-10-03"), 
                                          xend = as.Date("2020-10-03"), 
                                          y = ~min(both_yminc), yend = ~max(both_ymaxc),
                                          opacity = 0.35,
                                          name = "</br> First Home Football Game",
                                                 "</br> 2020-10-03",
                                          hoverinfo = "text",
                                          text = "First Home Football Game",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfc_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#E7298A', size = 6, opacity = 0.65))

p_wrf_c
save(p_wrf_c, file = "./plotly_objs/p_wrf_c.rda")
save(wrfa_both, file = "./plotly_objs/wrfa_both.rda")
save(wrfb_both, file = "./plotly_objs/wrfb_both.rda")
save(wrfc_both, file = "./plotly_objs/wrfc_both.rda")
save(date_vec_botha, file = "./plotly_objs/date_vec_botha.rda")
save(date_vec_bothb, file = "./plotly_objs/date_vec_bothb.rda")
save(date_vec_bothc, file = "./plotly_objs/date_vec_bothc.rda")
save(both_ymina, file = "./plotly_objs/both_ymina.rda")
save(both_ymaxa, file = "./plotly_objs/both_ymaxa.rda")

save(both_yminb, file = "./plotly_objs/both_yminb.rda")
save(both_ymaxb, file = "./plotly_objs/both_ymaxb.rda")

save(both_yminc, file = "./plotly_objs/both_yminc.rda")
save(both_ymaxc, file = "./plotly_objs/both_ymaxc.rda")